package com.avic.rong.course.activity.otherActivity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.course.adapter.ClassMuLuBgRvAdapter;
import com.avic.rong.course.adapter.ClassMuLuQyRvAdapter;
import com.avic.rong.course.adapter.CourseMuLuAdapter;
import com.avic.rong.course.bean.PeiXunClassMuLuBean;
import com.avic.rong.course.showapi.PeiXunClassMuLuApi;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.StringUtil;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.example.minato.minastore.utils.CircleProgressDialog;
import com.orhanobut.logger.Logger;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 课时目录
 */
public class PeiXunClassMuLuActivity extends BaseActivity {



    public static final String COURSESID = "coursesId";
    public static final String TYPE = "type";
    public static final String ARG_FIRST_IMAGE = "first_image";


    @BindView(R.id.ll_root)
    LinearLayout llRoot;
    @BindView(R.id.class_mulu_bg_rv)
    RecyclerView classMuluBgRv;

    private ClassMuLuBgRvAdapter bgRvAdapter;
    private ClassMuLuQyRvAdapter qyRvAdapter;
    private String coursesId;
    private String type;
    private String firstImageUrl = ConstantsUtils.EMPTY_STR;

    private CourseMuLuAdapter courseMuLuAdapter;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_pei_xun_class_mu_lu;
    }

    @Override
    protected void initView() {

//        ButterKnife.bind(this);
        tvTitle.setText("课时目录");
        ivRight.setImageResource(R.mipmap.slh);

        coursesId = getIntent().getStringExtra(COURSESID);
        type = getIntent().getStringExtra(TYPE);
        firstImageUrl = getIntent().getStringExtra(ARG_FIRST_IMAGE);
        if (StringUtil.isEmpty(type) || StringUtil.isEmpty(coursesId)){

        }else {
            initData(coursesId, type);
        }
    }

    private void initData(String coursesId, String type) {
        PeiXunClassMuLuApi classMuLuApi = new PeiXunClassMuLuApi(this, simpleOnNextListener, coursesId, type, SharedPreferencesUtil.getLoginToken());
        classMuLuApi.setCache(false);
        classMuLuApi.setCancel(false);
        classMuLuApi.setShowProgress(true);
        Logger.i("----------------------------------------initData: type:" + type + "  courseId:" + coursesId + "---" +  SharedPreferencesUtil.getLoginToken());
        try {
            Logger.i("--------------------ddd--------------------initData: type:" + type + "  courseId:" + coursesId + "---" +  URLEncoder.encode(SharedPreferencesUtil.getLoginToken(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HttpManager.getInstance().doHttpDeal(classMuLuApi);
      /*  if (context!=null){
            CircleProgressDialog.showDialog(context);
        }*/
    }

    //获取课时目录列表数据监听
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(PeiXunClassMuLuActivity.this, subjects.getMsg());
                return;
            }

            PeiXunClassMuLuBean bean = (PeiXunClassMuLuBean) subjects.getResult();

//            List<PeiXunClassMuLuBean.DatasBean.HourListBean> bgList = bean.getDatas().get(0).getHourList();
//            if (bgList.size()>0){
//                classMuluBgRv.setLayoutManager(new LinearLayoutManager(PeiXunClassMuLuActivity.this));
//                bgRvAdapter = new ClassMuLuBgRvAdapter(PeiXunClassMuLuActivity.this,bgList);
//                classMuluBgRv.setAdapter(bgRvAdapter);
//            }

            courseMuLuAdapter = new CourseMuLuAdapter(PeiXunClassMuLuActivity.this,bean.getDatas());
            courseMuLuAdapter.setFirstImageUrl(firstImageUrl);
            classMuluBgRv.setLayoutManager(new LinearLayoutManager(PeiXunClassMuLuActivity.this));
            DividerItemDecoration divider = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
            divider.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider_10_pix));
            classMuluBgRv.addItemDecoration(divider);
            classMuluBgRv.setAdapter(courseMuLuAdapter);
           /* CircleProgressDialog.closeDialog();*/

//            if (bean.getDatas().size() >=2 ) {
//                List<PeiXunClassMuLuBean.DatasBean.HourListBean> qyList = bean.getDatas().get(1).getHourList();
//
//                if (qyList.size() > 0) {
//                    classMuluQyRv.setLayoutManager(new LinearLayoutManager(PeiXunClassMuLuActivity.this));
//                    qyRvAdapter = new ClassMuLuQyRvAdapter(PeiXunClassMuLuActivity.this, qyList);
//                    classMuluQyRv.setAdapter(qyRvAdapter);
//                }
//            }
        }

        @Override
        public void onError(final Throwable e) {
           /* CircleProgressDialog.closeDialog();*/
            Logger.i("cjx", "培训课时目录列表数据：" + e.getMessage());
        }
    };



    @OnClick(R.id.iv_back)
    public void goBack(View view){
        onBackPressed();
    }

    @OnClick(R.id.iv_right)
    public void goDirect(View view){
        showPopup(PeiXunClassMuLuActivity.this, llRoot);
    }

    public static Intent getIntent(Context context,String coursesId,String type,String firstImageUrl){
        Intent intent = new Intent(context, PeiXunClassMuLuActivity.class);
        intent.putExtra(COURSESID,coursesId);
        intent.putExtra(TYPE,type);
        intent.putExtra(ARG_FIRST_IMAGE,firstImageUrl);
        return intent;
    }


    @Override
    protected void addListener() {

    }
}
